home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / devs / new8n1 / 8n1_000.s next >
Text File  |  1980-12-01  |  61KB  |  1,989 lines

  1. ********************************************************************************
  2. **                                          **
  3. **  Name       : 8n1_000.s                              **
  4. **  Copyright  : © Copyright 96-97                          **
  5. **  Author     : Iain Barclay                              **
  6. **  Created    : 15 Jul 97                              **
  7. **  Version    : 43.3                                  **
  8. **                                          **
  9. ********************************************************************************
  10.      ;
  11.      ;      SNMA specific options
  12.      ;
  13.      IFD      SNMA
  14.      ;
  15.      CPU      M68010
  16.      ;
  17.      SNMAOPT  Q,A,M,T,E-
  18.      ;
  19.      ELSE
  20.      IFD PHXASS
  21.      machine 68010
  22.      ENDC
  23.      ENDC
  24.      ;
  25.      ;
  26. NEWCODE  SET    1
  27. SETDSR     SET    0
  28. NCOMM     SET    0
  29. FASTVBR  SET    1
  30. DANGER     SET    0
  31. NEGCHECK SET    1
  32. EOFCODE  SET    1
  33.      ;
  34.      ;
  35.      ;
  36.      SECTION  text,CODE
  37.      ;
  38.      ;
  39.      ;
  40. DEVICES_SERIAL_I_OBSOLETE  EQU        1
  41.      ;
  42.      ;
  43.      ;
  44.      INCLUDE  "exec/lists.i"
  45.      INCLUDE  "exec/memory.i"
  46.      INCLUDE  "exec/resident.i"
  47.      INCLUDE  "exec/devices.i"
  48.      INCLUDE  "exec/execbase.i"
  49.      INCLUDE  "exec/io.i"
  50.      INCLUDE  "exec/ports.i"
  51.      INCLUDE  "exec/errors.i"
  52.      INCLUDE  "exec/initializers.i"
  53.      INCLUDE  "intuition/preferences.i"
  54.      INCLUDE  "devices/timer.i"
  55.      INCLUDE  "devices/serial.i"
  56.      INCLUDE  "hardware/custom.i"
  57.      INCLUDE  "hardware/cia.i"
  58.      INCLUDE  "hardware/intbits.i"
  59.      INCLUDE  "hardware/adkbits.i"
  60.      INCLUDE  "resources/misc.i"
  61.      INCLUDE  "exec/alerts.i"
  62.      INCLUDE  "exec/macros.i"
  63.      INCLUDE  "8n1.device_rev.i"
  64.      ;
  65.      ;      Define hardware references
  66.      ;
  67.      XREF      _custom
  68.      XREF      _intena,_intenar,_intreq,_intreqr
  69.      XREF      _ciab,_ciabpra
  70.      XREF      _serper,_serdat,_serdatr
  71.      XREF      _adkcon,_adkconr
  72.      ;
  73.      ;      Exec Functions
  74.      ;
  75.      XREF      _LVORemDevice,_LVOOpenDevice,_LVOCloseDevice
  76.      XREF      _LVOSupervisor
  77.      XREF      _LVOAllocMem,_LVOFreeMem,_LVOCopyMem,_LVOTypeOfMem
  78.      XREF      _LVOReplyMsg
  79.      XREF      _LVOSendIO,_LVOAbortIO
  80.      XREF      _LVODisable,_LVOEnable
  81.      XREF      _LVOFindName
  82.      XREF      _LVOOpenResource
  83.      XREF      _LVOAddIntServer,_LVORemIntServer
  84.      XREF      _LVOSetIntVector
  85.      XREF      _LVOAlert
  86.      XREF      _LVOOpenLibrary,_LVOCloseLibrary
  87.      XREF      _LVOCacheClearU
  88.      ;
  89.      ;      Misc Resource Functions
  90.      ;
  91.      XREF      _LVOAllocMiscResource,_LVOFreeMiscResource
  92.      ;
  93.      ;      Intuition Functions
  94.      ;
  95.      XREF      _LVOGetPrefs
  96.      ;
  97.      ;      Mask used to get rid of the printer bits.
  98.      ;
  99. PRTMASK  EQU      (CIAF_PRTRPOUT|CIAF_PRTRBUSY)
  100.      ;
  101.      ;      Autovector offsets
  102.      ;
  103. LVL1VEC  EQU      (1-1)*4+$64
  104. LVL5VEC  EQU      (5-1)*4+$64
  105. VBR_SIZE EQU      $400
  106.      ;
  107.      ;  New Style device stuff (not the best way but it will do for now)
  108.      ;  if u do not have new Stlye stuff defined elsewhere remove these ;
  109. ;NSCMD_DEVICEQUERY   EQU $4000
  110. ; STRUCTURE NSDEVICEQUERYRESULT,0
  111. ;    ULONG   DEVQUERYFORMAT
  112. ;    ULONG   SIZEAVAILABLE
  113. ;    UWORD   DEVICETYPE
  114. ;    UWORD   DEVICESUBTYPE
  115. ;    APTR    SUPPORTEDCOMMANDS
  116. ;    LABEL   NSDEVICEQUERYRESULT_SIZE
  117. ;NSDEVTYPE_SERIAL    EQU 11
  118.      ;
  119.      ;      Device base
  120.      ;
  121.      STRUCTURE Base8n1,LIB_SIZE
  122.      UBYTE      vb_SaveDDRA
  123.      UBYTE      vb_SavePRA
  124.      APTR      vb_MiscBase
  125.      APTR      vb_OldLevel1
  126.      APTR      vb_OldLevel5
  127.      ULONG      vb_SegList
  128.      ULONG      vb_DefBaud
  129.      ULONG      vb_DefRBufLen
  130.      ULONG      vb_CurRBuf
  131.      ULONG      vb_CurRBufLen
  132.      ULONG      vb_CurBaud
  133.      UBYTE      vb_SerFlags
  134.      UBYTE      vb_Initialized
  135.      LABEL      sizeof_Base8n1
  136.      ;
  137. Start:
  138.      moveq      #-1,d0                  ; set return code
  139.      rts                          ; return
  140.      ;
  141.      ;      RamLib looks for this romtag
  142.      ;
  143. ROMTag     DC.W      RTC_MATCHWORD               ; RT_MATCHWORD
  144.      DC.L      ROMTag                  ; RT_MATCHTAG
  145.      DC.L      ENDTag                  ; RT_ENDSKIP
  146.      DC.B      RTF_AUTOINIT                  ; RT_FLAGS
  147.      DC.B      VERSION                  ; RT_VERSION
  148.      DC.B      NT_DEVICE                  ; RT_TYPE
  149.      DC.B      0                      ; RT_PRI
  150.      DC.L      Name                      ; RT_NAME
  151.      DC.L      IdString                  ; RT_IDSTRING
  152.      DC.L      Init                      ; RT_INIT
  153.      ;
  154.      ;      Perform device initialization
  155.      ;
  156. InitRoutine:
  157.      exg      d0,a0                   ; swap seglist and base
  158.      move.l   d0,vb_SegList(a0)              ; store seglist in base
  159.      move.l   a6,SysBase                  ; store in global storage
  160.      exg      a0,d0                   ; swap them back
  161.      rts                          ; return
  162.      ;
  163.      ;
  164.      ;
  165. dev_Open:
  166.      addq.w   #1,LIB_OPENCNT(a6)              ; fake open so device is not expunged
  167.      move.l   a5,-(sp)                  ; save registers
  168.      movea.l  a6,a5                   ; save base
  169.      ;
  170.      cmp.w      #IOEXTSER_SIZE,MN_LENGTH(a1)          ; IORequest size >= IOEXTSER_SIZE
  171.      bcs.w      50$                      ; nope, finish
  172.      ;
  173.      tst.l      d0                      ; unit 0 specified?
  174.      bne.w      50$                      ; nope, error (who cares?)
  175.      ;
  176.      cmpi.w   #1,LIB_OPENCNT(a5)              ; currently open?
  177.      bne.w      10$                      ; yep, go process
  178.      ;
  179.      move.b   IO_SERFLAGS(a1),vb_SerFlags(a5)     ; save flags
  180.      ;
  181.      tst.b      vb_Initialized(a5)              ; already initialized?
  182.      bne.w      40$                      ; yep, skip initialization
  183.      ;
  184.      movea.l  SysBase(pc),a6              ; get ExecBase
  185.      move.l   a1,-(sp)                  ; save register
  186.      ;
  187.      lea.l      miscresource(pc),a1              ; ptr to resource name
  188.      jsr      _LVOOpenResource(a6)              ; go open it
  189.      move.l   d0,vb_MiscBase(a5)              ; save base
  190.      ;
  191.      moveq      #MR_SERIALPORT,d0              ; set unit number
  192.      bsr.w      allocResource               ; go allocate it
  193.      tst.l      d0                      ; did we get it?
  194.      bne.b      20$                      ; nope, error
  195.      ;
  196.      moveq      #MR_SERIALBITS,d0              ; set unit number
  197.      bsr.w      allocResource               ; go allocate it
  198.      tst.l      d0                      ; did we get it?
  199.      bne.b      5$                     ; nope, error
  200.      ;
  201.      ;      Get system preferences
  202.      ;
  203.      lea.l      intuitlib(pc),a1              ; ptr to library name
  204.      moveq      #0,d0                   ; can't use OldOpenLibrary as this will not be supported.
  205.      jsr      _LVOOpenLibrary(a6)              ; go open it (any version)
  206.      movea.l  d0,a6                   ; get intuition base
  207.      ;
  208.      move.l   #(pf_SerParShk+3)&$fffffffc,d0      ; size we need (aligned)
  209.      suba.l   d0,sp                   ; reserve space
  210.      ;
  211.      movea.l  sp,a0                   ; set data area ptr
  212.      jsr      _LVOGetPrefs(a6)              ; get preferences
  213.      ;
  214.      movea.l  a6,a1                   ; get intuition base
  215.      movea.l  SysBase(pc),a6              ; restore ExecBase
  216.      jsr      _LVOCloseLibrary(a6)              ; close it
  217.      ;
  218.      moveq      #$0f,d0                  ; set mask
  219.      and.b      pf_SerStopBuf(sp),d0              ; get bufsize index
  220.      moveq      #0,d1                   ; clear upper half
  221.      addq.l   #8,d0                   ; calc shift value
  222.      move.w   pf_BaudRate(sp),d1              ; get baud rate
  223.      lsl.l      #2,d0                   ; get default bufsize
  224.      move.l   d0,vb_DefRBufLen(a5)              ; and store
  225.      ;
  226.      add.l      d1,d1                   ; generate offset
  227.      move.w   .baudTable(pc,d1.w),d1          ; get default baud
  228.      move.l   d1,vb_DefBaud(a5)              ; and store
  229.      ;
  230.      bsr      internalReset               ; go init baud and buffer
  231.      ;
  232.      lea.l      (pf_SerParShk+3)&$fffffffc(sp),sp   ; restore stack
  233.      tst.l      d0                      ; got 'em?
  234.      beq.b      30$                      ; yep, branch
  235.      ;
  236.      moveq      #MR_SERIALBITS,d0              ; set unit
  237.      movea.l  vb_MiscBase(a5),a6              ; get MiscBase
  238.      jsr      _LVOFreeMiscResource(a6)          ; release the resource
  239.      ;
  240. 5$     moveq      #MR_SERIALPORT,d0              ; set unit
  241.      movea.l  vb_MiscBase(a5),a6              ; get MiscBase
  242.      jsr      _LVOFreeMiscResource(a6)          ; release the resource
  243.      ;
  244. 20$     moveq      #SerErr_DevBusy,d0              ; set error status
  245.      move.l   (sp)+,a1                  ; restore registers
  246.      bra      50$                      ; go return
  247.      ;
  248.      ;      Preferences baud lookup table
  249.      ;
  250. .baudTable:
  251.      dc.w      112,300,1200,2400,4800,9600,19200,31250,38400,57600,62400,64800
  252.      ;
  253. 30$     moveq      #0,d1                   ; clear flags
  254.      moveq      #UNIT_VBLANK,d0              ; set unit
  255.      lea.l      timerReq(pc),a1              ; ptr to timer request
  256.      lea.l      timerdevice(pc),a0              ; ptr to device name
  257.      jsr      _LVOOpenDevice(a6)              ; go open it
  258.      ;
  259.     IFNE DANGER
  260.      move.w   #$04000,_intena
  261.      addq.b   #1,IDNestCnt(a6)              ; disable interrupts
  262.     ELSE
  263.      jsr      _LVODisable(a6)              ; disable interrupts
  264.     ENDC
  265.      ;
  266.      lea.l      _ciab,a1                  ; get ptr to ciab
  267.      move.b   ciaddra(a1),d0
  268.      move.b   d0,vb_SaveDDRA(a5)              ; save DDR value
  269.      andi.b   #PRTMASK,d0                  ; make serial bits input
  270.      move.b   (a1),d1                  ; ciapra(a1)
  271.      ori.b      #CIAF_COMDTR|CIAF_COMRTS,d0          ; make DTR/RTS output
  272.      move.b   d1,vb_SavePRA(a5)              ; save PR value
  273.      andi.b   #CIAF_COMDTR|CIAF_COMRTS|PRTMASK,d0 ; turn on DTR/RTS
  274.      move.b   d0,ciaddra(a1)
  275.      andi.b   #CIAF_COMCTS|CIAF_COMDSR|PRTMASK,d1 ; make CTS/DSR input
  276.      move.b   d1,(a1)                  ; ciapra(a1)
  277.      ;
  278.      moveq      #INTB_PORTS,d0              ; get interrupt number
  279.      lea.l      VBInterrupt(pc),a1              ; get interrupt ptr
  280.      jsr      _LVOAddIntServer(a6)              ; add it to the list
  281.      ;
  282.      bsr      getVBR                  ; get vector base (in A0)
  283.      ;
  284.     IFNE FASTVBR
  285.      btst      #AFB_68010,AttnFlags+1(a6)          ; 68010 or higher?
  286.      beq.b      nocpu$                  ; nope, branch
  287.      move.l   a5,-(sp)
  288.      move.l   a0,-(sp)
  289.      move.l   a0,a1
  290.      jsr      _LVOTypeOfMem(a6)
  291.      btst      #MEMB_FAST,d0               ; VBR in fast?
  292.      bne.b      nofast$                  ; yep, branch
  293.      move.l   #VBR_SIZE,d0
  294.      moveq      #MEMF_PUBLIC|MEMF_FAST,d1
  295.      jsr      _LVOAllocMem(a6)              ; Alloc fast-ram for VBR
  296.      tst.l      d0
  297.      beq.b      nofast$                  ; no fast, branch
  298.      move.l   (sp),a0                  ; get VBR
  299.      move.l   d0,a1
  300.      move.l   d0,-(sp)                  ; save new VBR
  301.      move.l   #VBR_SIZE,d0
  302.      jsr      _LVOCopyMem(a6)
  303.      move.l   (sp)+,a0                  ; get new VBR
  304.      lea      setVBR$(pc),a5
  305.      jsr      _LVOSupervisor(a6)              ; set new VBR
  306.      jsr      _LVOCacheClearU(a6)              ; clear cache
  307.      move.l   (sp)+,d0                  ; was old VBR zero?
  308.      beq.b      waszero$                  ; yep, branch (don't free)
  309.      move.l   d0,a1
  310.      move.l   #VBR_SIZE,d0
  311.      jsr      _LVOFreeMem(a6)              ; free old VBR
  312. waszero$
  313.      bsr      getVBR
  314.      bra.b      vbend$
  315. setVBR$
  316.      movec      a0,vbr                  ; set VBR
  317.      rte
  318. nofast$
  319.      move.l   (sp)+,a0                  ; restore VBR
  320. vbend$
  321.      move.l   (sp)+,a5                  ; restore a5
  322. nocpu$
  323.     ENDC
  324.      move.l   LVL1VEC(a0),vb_OldLevel1(a5)          ; save original vector
  325.      lea.l      level1(pc),a1               ; get new vector ptr
  326.      move.l   a1,LVL1VEC(a0)              ; set new vector
  327.      ;
  328.      move.l   LVL5VEC(a0),vb_OldLevel5(a5)          ; save original vector
  329.      lea.l      level5(pc),a1               ; get new vector ptr
  330.      move.l   a1,LVL5VEC(a0)              ; set new vector
  331.      ;
  332.      lea.l      _custom+intena,a1              ; get ptr to custom chips
  333.      move.w   #INTF_RBF|INTF_TBE,intreq-intena(a1); clear pending interrupts
  334.      move.w   #INTF_SETCLR|INTF_RBF|INTF_TBE,(a1) ; enable RBF & TBE
  335.      ;
  336.      addq.b   #1,vb_Initialized(a5)           ; set flag
  337.      ;
  338.     IFNE DANGER
  339.      subq.b   #1,IDNestCnt(a6)
  340.      bge.b      enable$
  341.      move.w   #$0C000,(a1)                  ; enable interrupts
  342. enable$  move.l   (sp)+,a1                  ; restore register
  343.      bra.b      40$
  344.     ELSE
  345.      move.l   (sp)+,a1                  ; restore register
  346.      pea      40$(pc)
  347.      jmp      _LVOEnable(a6)              ; enable interrupts
  348.      ; go exit
  349.     ENDC
  350.      ;
  351. 10$     moveq      #SerErr_DevBusy,d0              ; preset error status
  352.      btst      #SERB_SHARED,vb_SerFlags(a5)          ; opened shared?
  353.      beq.b      50$                      ; nope, error
  354.      btst      #SERB_SHARED,IO_SERFLAGS(a1)          ; requesting shared?
  355.      beq.b      50$                      ; nope, error
  356.      ;
  357.      ;      Initialize I/O request
  358.      ;
  359. 40$     moveq      #8,d0                   ; get char size
  360.      move.b   d0,IO_READLEN(a1)              ; set read length
  361.      move.b   d0,IO_WRITELEN(a1)              ; set write length
  362.      moveq      #1,d0                   ; get stop bits
  363.      move.b   d0,IO_STOPBITS(a1)              ; set stop bits
  364.      move.b   IO_SERFLAGS(a1),d0
  365.      ori.b      #SERF_XDISABLED|SERF_RAD_BOOGIE|SERF_QUEUEDBRK|SERF_7WIRE,d0 ;flags
  366.      andi.b   #~(SERF_PARTY_ODD|SERF_PARTY_ON),d0 ; not used
  367.      move.b   d0,IO_SERFLAGS(a1)
  368.      move.l   vb_CurBaud(a5),IO_BAUD(a1)          ; set baud
  369.      move.l   vb_CurRBufLen(a5),IO_RBUFLEN(a1)    ; set read buffer length
  370.      ;
  371.      addq.w   #1,LIB_OPENCNT(a5)              ; incr open count
  372.      andi.b   #~(1<<LIBB_DELEXP),LIB_FLAGS(a5)    ; clear expunge bit
  373.      moveq      #0,d0                   ; no error
  374.      ;
  375. 50$     move.b   d0,IO_ERROR(a1)              ; store error code
  376.      move.l   a5,a6                   ; restore base
  377.      move.l   (sp)+,a5                  ; restore registers
  378.      subq.w   #1,LIB_OPENCNT(a6)              ; remove fake open
  379.      rts                          ; return
  380.      ;
  381.      ;      Attempt to allocate one of the serial resources.
  382.      ;
  383. allocResource:
  384.      move.l   a6,-(sp)                  ; save base pointer
  385.      move.l   d0,-(sp)                  ; save unit
  386.      lea.l      Name(pc),a1                  ; get lock name
  387.      move.l   vb_MiscBase(a5),a6              ; get MiscBase
  388.      jsr      _LVOAllocMiscResource(a6)          ; go allocate it
  389.      tst.l      d0                      ; did we get it?
  390.      beq.b      20$                      ; yep, branch
  391.      ;
  392.      ;      It's in use, so we try to locate the device using the string
  393.      ;      returned and attempt to remove it.
  394.      ;
  395.      movea.l  SysBase(pc),a6              ; get ExecBase
  396.      ;
  397.      movea.l  d0,a1                   ; get ptr to serial name
  398.      lea.l      DeviceList(a6),a0              ; get ptr to device list
  399.      jsr      _LVOFindName(a6)              ; go find it
  400.      tst.l      d0                      ; found?
  401.      beq.b      10$                      ; nope, branch
  402.      ;
  403.      movea.l  d0,a1                   ; xfer device ptr
  404.      jsr      _LVORemDevice(a6)              ; remove it
  405.      ;
  406.      ;      We then retry the allocate.
  407.      ;
  408. 10$     move.l   (sp),d0                  ; get unit
  409.      lea.l      Name(pc),a1                  ; get lock name
  410.      movea.l  vb_MiscBase(a5),a6              ; get MiscBase
  411.      jsr      _LVOAllocMiscResource(a6)          ; go allocate it
  412.      ;
  413. 20$     addq.w   #4,sp                   ; restore stack ptr
  414.      movea.l  (sp)+,a6                  ; restore base ptr
  415.      rts                          ; return
  416.      ;
  417.      ;      Set exception vectors
  418.      ;
  419. getVBR:
  420.      suba.l   a0,a0                   ; ptr to vector base (68000)
  421.      btst      #AFB_68010,AttnFlags+1(a6)          ; 68010 or higher?
  422.      beq.b      10$                      ; nope, go set vector
  423.      move.l   a5,-(sp)                  ; save registers
  424.      lea.l      20$(pc),a5                  ; ptr to routine
  425.      jsr      _LVOSupervisor(a6)              ; get into supervisor state
  426.      movea.l  (sp)+,a5                  ; restore register
  427. 10$     rts                          ; return
  428. 20$     movec.l  vbr,a0                  ; get vector base
  429.      rte                          ; return
  430.      ;
  431.      ;      Device Close routine
  432.      ;
  433. dev_Close:
  434.      ;
  435.      moveq      #-1,d0                  ; invalidate
  436.      move.l   d0,IO_DEVICE(a1)              ;   device
  437.      ;
  438.      tst.w      LIB_OPENCNT(a6)              ; check we are open
  439.      beq.b      5$                      ; nope, branch
  440.      subq.w   #1,LIB_OPENCNT(a6)              ; decr open count
  441.      bne.w      dev_Null                  ; still open? yep, branch
  442. 5$
  443.      ;
  444.      move.l   a5,-(sp)                  ; save registers
  445.      movea.l  a6,a5                   ; save base
  446.      movea.l  SysBase(pc),a6              ; get ExecBase
  447.      ;
  448.      move.l   a1,-(sp)                  ; save registers
  449.      ;
  450.      bsr.b      getVBR                  ; get vector base (in A0)
  451.      ;
  452.      moveq      #1,d0                   ; set not restored code
  453.      ;
  454.      lea.l      level1(pc),a1               ; get our vector ptr
  455.      cmpa.l   LVL1VEC(a0),a1              ; do they match?
  456.      bne      10$                      ; nope, can't restore
  457.      ;
  458.      lea.l      level5(pc),a1               ; get our vector ptr
  459.      cmpa.l   LVL5VEC(a0),a1              ; do they match?
  460.      bne      10$                      ; nope, can't restore
  461.      ;
  462.     IFNE DANGER
  463.      move.w   #$04000,_intena
  464.      addq.b   #1,IDNestCnt(a6)              ; disable interrupts
  465.     ELSE
  466.      jsr      _LVODisable(a6)              ; disable interrupts
  467.     ENDC
  468.      ;
  469.      move.l   vb_OldLevel1(a5),LVL1VEC(a0)          ; restore original vector
  470.      move.l   vb_OldLevel5(a5),LVL5VEC(a0)          ; restore original vector
  471.      ;
  472.      moveq      #INTB_PORTS,d0              ; get interrupt number
  473.      lea.l      VBInterrupt(pc),a1              ; get interrupt ptr
  474.      jsr      _LVORemIntServer(a6)              ; remove it from the list
  475.      ;
  476.      lea.l      _custom+intena,a0
  477.      move.w   #INTF_RBF|INTF_TBE,d0           ; indicate serial interrupts
  478.      move.w   d0,(a0)                  ; disable interrupts
  479.      move.w   d0,intreq-intena(a0)              ; clear pending interrupts
  480.      ;
  481.      bsr      freeBuf                  ; free allocated buffers
  482.      ;
  483.      lea.l      _ciab,a0                  ; get pointer to ciab
  484.      move.b   ciaddra(a0),d0              ; get DDR value
  485.      andi.b   #PRTMASK,d0                  ; save printer bits
  486.      ori.b      #~PRTMASK,d0                  ; set serial bits to output
  487.      move.b   d0,ciaddra(a0)              ; store value
  488.      ;
  489.      move.b   (a0),d0                  ; get PR value ( ciapra(a0) )
  490.      andi.b   #PRTMASK,d0                  ; mask out serial bits
  491.      move.b   vb_SavePRA(a5),d1              ; get saved value
  492.      andi.b   #~PRTMASK,d1                  ; mask out printer bits
  493.      or.b      d1,d0                   ; combine the two
  494.      move.b   d0,(a0)                  ; and store ( ciapra(a0) )
  495.      ;
  496.      move.b   ciaddra(a0),d0              ; get DDR value
  497.      andi.b   #PRTMASK,d0                  ; mask out serial bits
  498.      move.b   vb_SaveDDRA(a5),d1              ; get saved value
  499.      andi.b   #~PRTMASK,d1                  ; mask out printer bits
  500.      or.b      d1,d0                   ; combine the two
  501.      move.b   d0,ciaddra(a0)              ; and store
  502.      ;
  503.      lea.l      timerReq(pc),a1              ; get ptr to timer request
  504.      jsr      _LVOCloseDevice(a6)              ; go close it
  505.      ;
  506.      movea.l  vb_MiscBase(a5),a6              ; get MiscBase
  507.      moveq      #MR_SERIALBITS,d0              ; set unit
  508.      jsr      _LVOFreeMiscResource(a6)          ; release the resource
  509.      ;
  510.      moveq      #MR_SERIALPORT,d0              ; set unit
  511.      jsr      _LVOFreeMiscResource(a6)          ; release the resource
  512.      movea.l  SysBase(pc),a6              ; restore ExecBase
  513.      ;
  514.     IFNE DANGER
  515.      subq.b   #1,IDNestCnt(a6)
  516.      bge.b      enable$
  517.      move.w   #$0C000,_intena              ; enable interrupts
  518. enable$
  519.     ELSE
  520.      jsr      _LVOEnable(a6)              ; enable interrupts
  521.     ENDC
  522.      ;
  523.      subq.b   #1,vb_Initialized(a5)           ; clear flag
  524.      moveq      #0,d0                   ; free up everything
  525.      ;
  526. 10$     movea.l  (sp)+,a1                  ; restore registers
  527.      ;
  528.      movea.l  a5,a6                   ; restore base
  529.      movea.l  (sp)+,a5                  ; restore registers
  530.      ;
  531.      tst.l      d0                      ; freed?
  532.      bne.b      dev_Null                  ; nope, can't expunge, exit
  533.      ;
  534.      clr.b      vb_SerFlags(a6)              ; clear flags
  535.      ;
  536.      btst      #LIBB_DELEXP,LIB_FLAGS(a6)          ; delayed expunge set?
  537.      beq.b      dev_Null                  ; nope, go exit
  538.      ;
  539.      ;      Device Expunge routine (also fall through from dev_Close)
  540.      ;
  541. dev_Expunge:
  542.      ori.b      #1<<LIBB_DELEXP,LIB_FLAGS(a6)       ; Set expunge flag
  543.      tst.w      LIB_OPENCNT(a6)              ; currently open?
  544.      bne.b      dev_Null                  ; yep, so just exit
  545.      ;
  546.      move.l   vb_SegList(a6),d0              ; get seglist ptr
  547.      move.l   d0,-(sp)                  ; save registers (save D0!)
  548.      ;
  549.      movea.l  a6,a1                   ; get base
  550.      ; This is the REMOVE macro
  551.      move.l   (a1)+,a0
  552.      move.l   (a1),a1                  ; LN+PRED
  553.      move.l   a0,(a1)
  554.      move.l   a1,LN_PRED(a0)
  555.      ;
  556.      movea.l  a6,a1                   ; get base
  557.      moveq      #0,d0                   ; clear work
  558.      move.w   LIB_NEGSIZE(a6),d0              ; calculate
  559.      suba.w   d0,a1                   ;   memory address
  560.      add.w      LIB_POSSIZE(a6),d0              ;     and size
  561.      move.l   a6,-(sp)                  ; save registers
  562.      movea.l  SysBase(pc),a6              ; get ExecBase
  563.      jsr      _LVOFreeMem(a6)              ; free it
  564.      ;
  565.      move.l   (sp)+,a6                  ; restore registers
  566.      move.l   (sp)+,d0                  ; restore registers
  567.      rts                          ; return (seglist in D0!)
  568.      ;
  569.      ;      Device "ExtFunc" routine
  570.      ;
  571. dev_Null:
  572.      moveq      #0,d0                   ; set return code
  573.      rts                          ; return
  574.      ;
  575. cmdTable dc.w      cmd_Invalid-cmdTable              ; CMD_INVALID
  576.      dc.w      cmd_Reset-cmdTable              ; CMD_RESET
  577.      dc.w      cmd_Read-cmdTable              ; CMD_READ
  578.      dc.w      cmd_Write-cmdTable              ; CMD_WRITE
  579.      dc.w      cmd_Invalid-cmdTable              ; CMD_UPDATE
  580.      dc.w      cmd_Clear-cmdTable              ; CMD_CLEAR
  581.      dc.w      cmd_Invalid-cmdTable              ; CMD_STOP
  582.      dc.w      cmd_Invalid-cmdTable              ; CMD_START
  583.      dc.w      cmd_Flush-cmdTable              ; CMD_FLUSH
  584.      dc.w      sdcmd_Query-cmdTable              ; SDCMD_QUERY
  585.      dc.w      sdcmd_Break-cmdTable              ; SDCMD_BREAK
  586.      dc.w      sdcmd_SetParams-cmdTable          ; SDCMD_SETPARAMS
  587. endTable
  588.      dc.w      nscmd_DeviceQuery-cmdTable          ; NSCMD_DEVICEQUERY
  589. nsendTable
  590.      ;
  591.      ;      Device BeginIO routine
  592.      ;
  593. dev_BeginIO:
  594.      move.l   a5,-(sp)                  ; save register
  595.      move.l   a6,a5                   ; save base
  596.      move.l   SysBase(pc),a6              ; get ExecBase
  597.      ;
  598.      move.b   #NT_MESSAGE,LN_TYPE(a1)          ; set type
  599.      clr.b      IO_ERROR(a1)                  ; clear error
  600.      ;
  601.      andi.b   #~(IOSERF_QUEUED|IOSERF_ACTIVE),IO_FLAGS(a1) ; clear flags
  602.      ;
  603.      move.w   IO_COMMAND(a1),d0              ; get command
  604.      add.w      d0,d0                   ; multiply by 2
  605.      moveq      #endTable-cmdTable,d1
  606.      cmp.w      d1,d0                   ; in range?
  607.      bcc.b      25$                      ; nope, branch
  608.      ;
  609. 5$     move.w   cmdTable(pc,d0.w),d0              ; get routine offset
  610.      jsr      cmdTable(pc,d0.w)              ; go do it
  611.      tst.l      d0                      ; I/O completed?
  612.      bne.b      19$                      ; nope, go return
  613.      ;
  614. 10$     btst      #IOB_QUICK,IO_FLAGS(a1)          ; need to reply?
  615.      bne.b      20$                      ; nope, branch
  616. 15$     jsr      _LVOReplyMsg(a6)              ; send it back
  617.      ;
  618. 19$     andi.b   #~(1<<IOB_QUICK),IO_FLAGS(a1)       ; clear quick bit
  619. 20$     movea.l  a5,a6                   ; restore base
  620.      movea.l  (sp)+,a5                  ; restore register
  621.      rts                          ; return
  622. 25$     cmp.w      #(2*NSCMD_DEVICEQUERY),d0
  623.      bne.b      30$
  624.      move.l   d1,d0
  625.      bra.b      5$
  626.      ;
  627. 30$     move.b   #IOERR_NOCMD,IO_ERROR(a1)          ; invalid command
  628.      bra.b      10$                      ; branch
  629.      ;
  630.      ;      Device AbortIO routine
  631.      ;
  632. dev_AbortIO:
  633.      move.l   a5,-(sp)                  ; save registers
  634.      movea.l  a6,a5                   ; save base
  635.      movea.l  SysBase(pc),a6              ; get ExecBase
  636.      ;
  637.     IFNE DANGER
  638.      move.w   #$04000,_intena
  639.      addq.b   #1,IDNestCnt(a6)              ; disable interrupts
  640.     ELSE
  641.      jsr     _LVODisable(a6)              ; disable interrupts
  642.     ENDC
  643.      ;
  644.      move.b   IO_FLAGS(a1),d1
  645.      btst      #IOSERB_QUEUED,d1              ; queued request?
  646.      bne.b      40$                      ; yep, branch
  647.      ;
  648.      btst      #1<<IOSERB_ACTIVE,d1              ; active request?
  649.      beq.b      10$                      ; nope, just exit
  650.      ;
  651.      move.w   IO_COMMAND(a1),d0              ; get command
  652.      subq.w   #CMD_READ,d0                  ; was it a read?
  653.      beq.b      20$                      ; yep, go process
  654.      ;
  655.      subq.w   #CMD_WRITE-CMD_READ,d0          ; was it a write?
  656.      beq.b      30$                      ; yep, go process
  657.      ;
  658.      subq.w   #SDCMD_BREAK-CMD_WRITE,d0          ; was it a break?
  659.      beq.b      30$                      ; yep, go process
  660.      ;
  661.      ;      Fall through or enter from below
  662.      ;
  663.     IFNE DANGER
  664. 10$     subq.b   #1,IDNestCnt(a6)
  665.      bge.b      enable$
  666.      move.w   #$0C000,_intena             ; enable ints and return
  667. enable$
  668.     ELSE
  669. 10$     jsr      _LVOEnable(a6)              ; enable ints and return
  670.     ENDC
  671. 15$     move.l   a5,a6                   ; restore base
  672.      move.l   (sp)+,a5                  ; restore registers
  673.      rts                          ; return
  674.      ;
  675.      ;      Abort an active read request
  676.      ;
  677. 20$     clr.l      cr_IOReq                  ; no longer active
  678.      bra.b      50$                      ; go set flags
  679.      ;
  680.      ;      Abort an active write request
  681.      ;
  682. 30$     clr.l      cw_Length                  ; no longer active
  683.      clr.l      cw_IOReq                  ; no longer active
  684.      move.l   cw_Buffer(pc),d0              ; get buffer ptr
  685.      sub.l      IO_DATA(a1),d0              ; calc number of bytes xfer'd
  686.      move.l   d0,IO_ACTUAL(a1)              ; store
  687.      ;
  688.      ;      Force a TBE interrupt to get the next write going.
  689.      ;
  690.      move.w   #INTF_SETCLR|INTF_TBE,_intreq       ; make TBE pending
  691.      bra.b      50$                      ; go set flags
  692.      ;
  693.      ;      Remove I/O from queue.
  694.      ;
  695. 40$     move.l   a1,-(sp)                  ; save ptr
  696.      ; This is the REMOVE macro
  697.      move.l   (a1)+,a0
  698.      move.l   (a1),a1                  ; LN+PRED
  699.      move.l   a0,(a1)
  700.      move.l   a1,LN_PRED(a0)
  701.      ;
  702.      movea.l  (sp)+,a1                  ; restore ptr
  703.      ;
  704.      ;      Set error and return I/O
  705.      ;
  706. 50$     move.b   #IOERR_ABORTED,IO_ERROR(a1)          ; set error code
  707.      move.b   IO_FLAGS(a1),d1              ; get flags
  708.      ori.b      #1<<IOSERB_ABORT,d1              ; set abort flag
  709.      andi.b   #~(IOSERF_QUEUED|IOSERF_ACTIVE),d1  ; clear flags
  710.      move.b   d1,IO_FLAGS(a1)              ; store flags
  711.      btst      #IOB_QUICK,d1               ; need to reply?
  712.      bne.b      10$                      ; nope, branch
  713.      pea      10$(pc)                  ; branch to return
  714.      jmp      _LVOReplyMsg(a6)              ; send it back
  715.      ;
  716.      ;      Abort all active/queued commands and reset internal state
  717.      ;
  718. cmd_Reset:
  719.      move.l   a1,-(sp)                  ; save I/O request
  720.      bsr.b      cmd_Flush                  ; go abort queued requests
  721.      ;
  722.     IFNE DANGER
  723.      move.w   #$04000,_intena
  724.      addq.b   #1,IDNestCnt(a6)              ; disable interrupts
  725.     ELSE
  726.      jsr      _LVODisable(a6)              ; disable interrupts
  727.     ENDC
  728.      ;
  729.      ;      This must follow the DISABLE
  730.      ;
  731.      exg      a5,a6                   ; exchange base and ExecBase
  732.      ;
  733.      move.l   cr_IOReq(pc),d0              ; active read?
  734.      beq.b      10$                      ; nope, branch
  735.      movea.l  d0,a1                   ; get I/O request
  736.      bsr      dev_AbortIO                  ; go abort it
  737.      ;
  738.      ;
  739.      ;
  740. 10$     move.l   cw_IOReq(pc),d0              ; active write?
  741.      beq.b      20$                      ; nope, branch
  742.      movea.l  d0,a1                   ; get I/O request
  743.      bsr      dev_AbortIO                  ; go abort it
  744.      ;
  745. 20$     exg      a5,a6                   ; restore base and ExecBase
  746.      movea.l  (sp)+,a1                  ; restore I/O request
  747.      moveq      #8,d0                   ; get char size
  748.      move.b   d0,IO_READLEN(a1)              ; set read length
  749.      move.b   d0,IO_WRITELEN(a1)              ; set write length
  750.      moveq      #1,d0                   ; get stop bits
  751.      move.b   d0,IO_STOPBITS(a1)              ; set stop bits
  752.      move.l   vb_DefBaud(a5),IO_BAUD(a1)          ; set to default baud
  753.      move.l   vb_DefRBufLen(a5),IO_RBUFLEN(a1)    ; set to default buflen
  754.      bsr      internalReset               ; go set/verify parameters
  755.      move.b   d0,IO_ERROR(a1)              ; set error code
  756.      ;
  757.      ;      Set RC
  758.      ;
  759.      moveq      #0,d0                   ; I/O complete
  760.     IFNE DANGER
  761.      subq.b   #1,IDNestCnt(a6)
  762.      bge.b      enable$
  763.      move.w   #$0C000,_intena              ; enable interrupts
  764. enable$  rts
  765.     ELSE
  766.      jmp      _LVOEnable(a6)              ; enable interrupts
  767.                               ; return
  768.     ENDC
  769.      ;
  770.      ;      Abort all "queued" requests, leaving all active alone.
  771.      ;
  772.  
  773. cmd_Flush:
  774.      move.l   a2,-(sp)                  ; save registers
  775.      move.l   a1,-(sp)                  ; save registers
  776.      ;
  777.      ;      Abort all queued read requests.
  778.      ;
  779.      lea.l      readQ(pc),a2                  ; get ptr to read queue
  780.      bsr.b      20$                      ; branch to abort
  781.      ;
  782.      ;      Abort all queued write requests.
  783.      ;
  784.      lea.l      writeQ(pc),a2               ; get ptr to write queue
  785.      bsr.b      20$                      ; branch to abort
  786.      ;
  787.      ;      restore, and return to caller
  788.      ;
  789.      move.l   (sp)+,a1                  ; restore registers
  790.      move.l   (sp)+,a2                  ; restore registers
  791.      ;
  792.      ;      Set RC and return
  793.      ;
  794.      moveq      #0,d0                   ; I/O complete
  795.      ;
  796.      ;      !!!NOTE!!!  In a mad attempt to save 2 bytes, this RTS is used
  797.      ;      by the subroutine below.  Why waste 'em?  B-)
  798.      ;
  799. 10$     rts                          ; return ( used below too!! )
  800.      ;
  801.      ;      Subroutine to remove and reply each I/O request.
  802.      ;
  803.     IFNE DANGER
  804. 20$     lea      custom+intena,a0
  805.      move.w   #$04000,(a0)
  806.      addq.b   #1,IDNestCnt(a6)              ; disable interrupts
  807.     ELSE
  808. 20$     jsr      _LVODisable(a6)              ; disable interrupts
  809.     ENDC
  810.      move.l   a2,8(a2)                  ; clear list
  811.      move.l   (a2)+,a1                  ; and get ptr to first I/O request
  812.      move.l   a2,-(a2)                  ;
  813.      move.l   a1,a2                   ; save I/O request in a2
  814.      ;
  815.     IFNE DANGER
  816.      subq.b   #1,IDNestCnt(a6)
  817.      bge.b      enable$
  818.      move.w   #$0C000,(a0)                  ; enable interrupts
  819. enable$
  820.     ELSE
  821.      jsr      _LVOEnable(a6)              ; enable interrupts
  822.     ENDC
  823. 25$     move.l   (a2),d0                  ; get next I/O request
  824.      beq.b      10$                      ; end of list? yep, branch to return
  825.      ;
  826.      pea.l      25$(pc)                  ; push return address
  827.      move.l   a2,a1                   ; move I/O request to a1
  828.      move.l   d0,a2                   ; save next I/O request in a2
  829.      andi.b   #~(1<<IOSERB_QUEUED),IO_FLAGS(a1)   ; no longer queued
  830.      moveq      #IOERR_ABORTED,d0              ; indicate aborted
  831.      move.b   d0,IO_ERROR(a1)              ; store status
  832.      ;
  833.      jmp      _LVOReplyMsg(a6)              ; send it back
  834.      ;
  835.      ;      Process a CMD_READ request.
  836.      ;
  837. cmd_Read:
  838.      ;
  839.      ;      Zero length requests just get returned.
  840.      ;
  841.      clr.l      IO_ACTUAL(a1)               ; clear bytes read
  842.      move.l   IO_LENGTH(a1),d0              ; get length and test
  843.      beq.b      20$                      ; yep, leave
  844.      ;
  845.      ;      This can be used to circumvent a bug in NComm 3.0 which
  846.      ;      references the buffer even when there was nothing read.
  847.      ;
  848.     IFNE     NCOMM
  849.      move.l   IO_DATA(A1),a0              ; get data pointer
  850.      clr.b      (a0)                      ; clear first byte in buffer
  851.     ENDC
  852.      ;
  853.      ;      The disable counter works just like exec's TDNestCnt field.  It's
  854.      ;      initialized to -1.  After incrementing, if it is 0, then we
  855.      ;      can attempt to process this request immediately.  If it's > 0,
  856.      ;      then we're already disabled and we must queue this request.
  857.      ;
  858.      addq.b   #1,disableRead              ; incr disable count
  859.      bgt.b      50$                      ; >0, already disabled
  860.      ;
  861.      ;      If we're already processing an request, this one has to wait
  862.      ;      until that one is done, so go queue it.
  863.      ;
  864.      move.l   cr_IOReq(pc),d1              ; have an active request?
  865.      bne.b      50$                      ; yep, go queue this one
  866.      ;
  867.      ;      If we don't have enough bytes to satisfy this request then go
  868.      ;      queue it.
  869.      ;
  870.      cmp.l      i_InCnt(pc),d0              ; length > current bytes?
  871.      bgt.b      50$                      ; yep, go queue it
  872.      ;
  873.      ;      Setup fields and go copy the data
  874.      ;
  875.     IFNE NCOMM
  876.      move.l   a0,cr_OutPtr                  ; get/set output ptr
  877.     ELSE
  878.      move.l   IO_DATA(a1),cr_OutPtr           ; get/set output ptr
  879.     ENDC
  880.      move.l   d0,cr_Length                  ; get/set output count
  881.      bsr      copyData                  ; go copy 'em
  882.      moveq      #0,d0                   ; I/O complete
  883.      ;
  884.      ;      We're done, so back off the disable counter.
  885.      ;
  886. 10$     subq.b   #1,disableRead              ; decr disable count
  887.      ;
  888.      ;      Return to caller
  889.      ;
  890. 20$     rts                          ; return
  891.      ;
  892.      ;      Just set flags and queue.  The read interrupt will handle it.
  893.      ;
  894. 50$     ori.b      #1<<IOSERB_QUEUED,IO_FLAGS(a1)      ; indicate queued
  895.      ;
  896.      ;      Add this request to the end.
  897.      ;
  898.     IFNE DANGER
  899.      move.w   #$04000,_intena
  900.      addq.b   #1,IDNestCnt(a6)              ; disable interrupts
  901.     ELSE
  902.      jsr      _LVODisable(a6)              ; disable interrupts
  903.     ENDC
  904.      lea      readQ(pc),a0                  ; get pointer to read queue
  905.      ; This is the ADDTAIL macro
  906.      addq.w   #LH_TAIL,a0
  907.      move.l   LN_PRED(a0),d0
  908.      move.l   a1,LN_PRED(a0)
  909.      exg      d0,a0
  910.      movem.l  d0/a0,(a1)
  911.      move.l   a1,(a0)
  912.      ;
  913.      ;      Indicate that this request was not handled immediatly.
  914.      ;
  915.      moveq      #1,d0                   ; I/O not complete
  916.      ;
  917.     IFNE DANGER
  918.      subq.b   #1,IDNestCnt(a6)
  919.      bge.b      10$
  920.      move.w   #$0C000,_intena              ; enable interrupts
  921.      bra.b      10$                      ; branch to return
  922.     ELSE
  923.      pea.l      10$(pc)                  ; branch to return
  924.      jmp      _LVOEnable(a6)              ; enable interrupts
  925.     ENDC
  926.      ;
  927.      ;      Process a CMD_WRITE request.
  928.      ;
  929. cmd_Write:
  930.      clr.l      IO_ACTUAL(a1)               ; clear bytes written
  931.      move.l   IO_LENGTH(a1),d0              ; get length
  932.      beq.b      inv_rt                  ; nope, proccess
  933.     IFNE EOFCODE
  934.      btst      #SERB_EOFMODE,IO_SERFLAGS(a1)       ; EOFMODE requested?
  935.      bne.b      eoferr                  ; yep, branch
  936.     ENDC
  937.     IFNE NEGCHECK
  938.      ;
  939.      ; Check for -1 length
  940.      ;
  941.      moveq      #-1,d1                  ; get value
  942.      cmp.l      d1,d0                   ; length = -1?
  943.      beq.b      negcheck                  ; nope, skip scan
  944.     ENDC
  945.      ;
  946.      ;      Entry point for Break command and fall through from cmd_Write.
  947.      ;
  948. sdcmd_Break:
  949.      ;
  950.      ;      Just set flags and queue. The TBE interrupt will handle it.
  951.      ;
  952.      ori.b      #1<<IOSERB_QUEUED,IO_FLAGS(a1)      ; indicate queued
  953.      ;
  954.      ;      Protect.
  955.      ;
  956.     IFNE DANGER
  957.      move.w   #$04000,_intena
  958.      addq.b   #1,IDNestCnt(a6)              ; disable interrupts
  959.     ELSE
  960.      jsr      _LVODisable(a6)              ; disable interrupts
  961.     ENDC
  962.      ;
  963.      ;      Add request to end of queue.
  964.      ;
  965.      lea.l      writeQ(pc),a0               ; get queue list ptr
  966.      ; This is the ADDTAIL macro
  967.      addq.w   #LH_TAIL,a0
  968.      move.l   LN_PRED(a0),d0
  969.      move.l   a1,LN_PRED(a0)
  970.      exg      d0,a0
  971.      movem.l  d0/a0,(a1)
  972.      move.l   a1,(a0)
  973.      ;
  974.      ;      If we have an active request, don't force interrupt.
  975.      ;
  976.      move.l   cw_IOReq(pc),d0              ; have an active request?
  977.      bne.b      10$                      ; yep, branch
  978.      ;
  979.      ;      Force a TBE interrupt to get the writes going.
  980.      ;
  981.      move.w   #INTF_SETCLR|INTF_TBE,_intreq       ; make TBE pending
  982.      ;
  983.      ;      Enable, set RC and return to caller.
  984.      ;
  985. 10$     moveq      #1,d0                   ; I/O not complete
  986.     IFNE DANGER
  987.      subq.b   #1,IDNestCnt(a6)
  988.      bge.b      enable$
  989.      move.w   #$0C000,_intena              ; enable interrupts
  990. enable$  rts                          ; return
  991.     ELSE
  992.      jmp      _LVOEnable(a6)              ; enable interrupts
  993.                               ; return
  994.     ENDC
  995.     IFNE EOFCODE
  996. eoferr:
  997.     moveq      #SerErr_InvParam,d0              ; SerErr_InvParam instead?
  998.     move.b      d0,IO_ERROR(a1)              ; Set error
  999.     moveq      #0,d0                   ; I/O Complete
  1000.     rts
  1001.     ENDC
  1002.     IFNE NEGCHECK
  1003.      ;
  1004.      ;      Scan the data for null to calc the length
  1005.      ;
  1006. negcheck:
  1007.      move.l   a0,d1                   ; save data ptr
  1008. 10$     tst.b      (a0)+                   ; does it equal 0? (first byte)
  1009.      bne.b      10$                      ; nope, loop
  1010.      move.l   a0,d0
  1011.      sub.l      d1,d0                   ; calc # of bytes
  1012.      move.l   d0,IO_LENGTH(a1)              ; save length
  1013.      bra.b      sdcmd_Break
  1014.     ENDC
  1015.      ;
  1016.      ;      Resets serial read buffer
  1017.      ;
  1018.      ;      Since this routine is called internally, it must NOT reference
  1019.      ;      the I/O request.
  1020.      ;
  1021. cmd_Clear:
  1022.     IFNE DANGER
  1023.      move.w   #$04000,_intena
  1024.      addq.b   #1,IDNestCnt(a6)              ; disable interrupts
  1025.     ELSE
  1026.      jsr      _LVODisable(a6)              ; disable interrupts
  1027.     ENDC
  1028.      ;
  1029.      ;      Load registers
  1030.      ;
  1031.      move.l   vb_CurRBuf(a5),d0              ; get internal buffer ptr
  1032.      move.l   vb_CurRBufLen(a5),d1              ; and internal buffer len
  1033.      lea.l      i_BufPtr(pc),a0              ; get ptr internal control
  1034.      ;
  1035.      ;      Initialize global buffer variables
  1036.      ;
  1037.      move.l   d0,(a0)+                  ; store buffer ptr
  1038.      move.l   d0,(a0)+                  ; set current input ptr
  1039.      move.l   d0,(a0)+                  ; set current output ptr
  1040.      add.l      d1,d0                   ; add buffer length
  1041.      move.l   d0,(a0)+                  ; store ptr to end of buffer
  1042.      clr.l      (a0)+                   ; clear byte cnt
  1043.      move.l   vb_CurBaud(a5),d0              ; get internal baud
  1044.      lsr.l      #4,d0                   ; divide by 16
  1045.      sub.l      d0,d1                   ; subtract from length
  1046.      move.l   d1,(a0)                  ; set threshold
  1047.      ;
  1048.      ;      Enable, set RC and return.
  1049.      ;
  1050.      moveq      #0,d0                   ; I/O complete
  1051.     IFNE DANGER
  1052.      subq.b   #1,IDNestCnt(a6)
  1053.      bge.b      enable$
  1054.      move.w   #$0C000,_intena              ; enable interrupts
  1055. enable$  rts                          ; return
  1056.     ELSE
  1057.      jmp      _LVOEnable(a6)              ; enable interrupts
  1058.                               ; return
  1059.     ENDC
  1060.      ;
  1061.      ;
  1062.      ;
  1063. cmd_Invalid
  1064.      move.b   #IOERR_NOCMD,IO_ERROR(a1)          ; set bad status
  1065.      moveq      #0,d0                   ; I/O complete
  1066. inv_rt     rts                          ; return
  1067.      ;
  1068.      ;      Returns number of bytes currently in internal buffer and
  1069.      ;      current serial port status.
  1070.      ;
  1071.      ;      NOTE:  Not completely compatible with standard serial.device
  1072.      ;      since it doesn't return the upper byte of IO_STATUS.
  1073.      ;
  1074. sdcmd_Query:
  1075.     IFNE DANGER
  1076.      lea      custom+intena,a0
  1077.      move.w   #$04000,(a0)
  1078.      addq.b   #1,IDNestCnt(a6)              ; disable interrupts
  1079.     ELSE
  1080.      jsr      _LVODisable(a6)              ; disable interrupts
  1081.     ENDC
  1082.      moveq      #0,d0                   ; clear d0
  1083.      move.b   _ciabpra,d0                  ; get PR register
  1084.      andi.b   #~PRTMASK,d0                  ; zap printer bits
  1085.      ;
  1086.      ;      Uncomment the following to turn on the DSR bit.  This was done
  1087.      ;      for a user whose DSR pin did not function.
  1088.      ;
  1089.     IFNE SETDSR
  1090.      andi.b   #~(1<<CIAB_COMDSR),d0             ; set DSR
  1091.     ENDC
  1092.      ;
  1093.      ;
  1094.      ;
  1095.      move.w   d0,IO_STATUS(a1)              ; store status
  1096.      move.l   i_InCnt(pc),IO_ACTUAL(a1)          ; byte left in buffer
  1097.      ;
  1098.      ;      Enable, set RC and return.
  1099.      ;
  1100.      moveq      #0,d0                   ; I/O complete
  1101.     IFNE DANGER
  1102.      subq.b   #1,IDNestCnt(a6)
  1103.      bge.b      enable$
  1104.      move.w   #$0C000,(a0)                  ; enable interrupts
  1105. enable$  rts                          ; return
  1106.     ELSE
  1107.      jmp      _LVOEnable(a6)              ; enable interrupts
  1108.                               ; return
  1109.     ENDC
  1110.      ;
  1111.      ;
  1112.      ;
  1113. sdcmd_SetParams:
  1114.      ;
  1115.      ;      Validate the read, write, and stop bit lengths.
  1116.      ;
  1117.      moveq      #8,d0                   ; get char length
  1118.      cmp.b      IO_READLEN(a1),d0              ; 8 bit chars for read?
  1119.      bne.b      40$                      ; nope, branch
  1120.      cmp.b      IO_WRITELEN(a1),d0              ; 8 bit chars for write?
  1121.      bne.b      40$                      ; nope, branch
  1122.      moveq      #1,d0                   ; get stop bits
  1123.      cmp.b      IO_STOPBITS(a1),d0              ; 1 stop bit?
  1124.      bne.b      40$                      ; nope, branch
  1125.      ;
  1126.      ;      Get and validate the baud rate.
  1127.      ;
  1128.      move.l   IO_BAUD(a1),d1              ; get baud from I/O req
  1129.      bne.b      20$                      ; specified?
  1130.      move.l   vb_CurBaud(a5),d1              ; get current baud from base
  1131.      bne.b      20$                      ; specified?
  1132.      move.l   vb_DefBaud(a5),d1              ; get default baud from base
  1133. 20$     moveq      #110,d0
  1134.      cmp.l      d0,d1                   ; too low?
  1135.      blt.b      40$                      ; error
  1136.      cmp.l      #292000,d1                  ; too high?
  1137.      bgt.b      40$                      ; error
  1138.      ;
  1139.      ;      Get and validate the buffer length.
  1140.      ;
  1141.      move.l   IO_RBUFLEN(a1),d0              ; get buffer length
  1142.      bne.b      30$                      ; specified?
  1143.      move.l   vb_CurRBufLen(a5),d0              ; get current from base
  1144.      bne.b      30$                      ; specified?
  1145.      move.l   vb_DefRBufLen(a5),d0              ; get default from base
  1146.      ;
  1147. 30$     bsr.b      internalReset               ; go init baud and buffer
  1148. 35$     move.b   d0,IO_ERROR(a1)              ; set error code
  1149.      bne.b      39$
  1150.      ;
  1151.      ;      If the 7wire bit is not on, we will only use 3-wire protocol
  1152.      ;
  1153.      moveq      #0,d0                   ; clear flag
  1154.      btst      #SERB_7WIRE,IO_SERFLAGS(a1)          ; use 7wire handshaking?
  1155.      beq.b      36$                      ; nope, branch
  1156.      moveq      #1,d0                   ; set flag
  1157. 36$     move.b   d0,Handshake                  ; store flag
  1158.      ;
  1159.      ;      Set RC and return.
  1160.      ;
  1161. 39$     moveq      #0,d0                   ; I/O complete
  1162.      rts                          ; return
  1163.      ;
  1164.      ;      Invalid parm detected.
  1165.      ;
  1166. 40$     moveq      #SerErr_InvParam,d0              ; set error
  1167.      bra.b      35$                      ; go return
  1168.      ;
  1169. nscmd_DeviceQuery:
  1170.      tst.l      IO_DATA(a1)
  1171.      beq.b      10$
  1172.      move.l   IO_DATA(a1),a0
  1173.      moveq      #NSDEVICEQUERYRESULT_SIZE,d0
  1174.      move.l   d0,SIZEAVAILABLE(a0)
  1175.      move.l   d0,IO_ACTUAL(a1)
  1176.      move.w   #NSDEVTYPE_SERIAL,DEVICETYPE(a0)
  1177.      move.l   a1,-(sp)
  1178.      lea.l      cmdlist(pc),a1
  1179.      move.l   a1,SUPPORTEDCOMMANDS(a0)
  1180.      move.l   (sp)+,a1
  1181. 10$     moveq      #0,d0
  1182.      rts
  1183.      ;
  1184.      ;      Reset the buffer and baud rate
  1185.      ;
  1186.      ;      Registers:  D0 = Buffer length
  1187.      ;              D1 = Baud rate
  1188.      ;
  1189. internalReset:
  1190.      ;
  1191.      ;      Disable interrupts.
  1192.      ;
  1193.     IFNE DANGER
  1194.      move.w   #$04000,_intena
  1195.      addq.b   #1,IDNestCnt(a6)              ; disable interrupts
  1196.     ELSE
  1197.      jsr      _LVODisable(a6)              ; disable interrupts
  1198.     ENDC
  1199.      ;
  1200.      ;      Save buffer length and go set serper.
  1201.      ;
  1202.      move.l   d0,-(sp)                  ; save D0
  1203.      move.l   d1,d0                   ; get baud rate
  1204.      ;
  1205.      ;      Set serial period register
  1206.      ;
  1207.      cmp.l      vb_CurBaud(a5),d0              ; current baud = new baud?
  1208.      beq.b      40$                      ; yep, just exit
  1209.      move.l   d0,vb_CurBaud(a5)              ; save new baud
  1210.      move.l   d0,d1                   ; save again
  1211.      lsl.l      #3,d0                   ; baud *= 8
  1212.      sub.l      d1,d0                   ; baud -= saved baud
  1213.      move.l   #25000000,d1                  ; get NTSC base
  1214.      cmpi.b   #50,PowerSupplyFrequency(a6)          ; PAL machine?
  1215.      bne.b      5$                      ; nope, branch
  1216.      move.l   #24772416,d1                  ; get PAL base
  1217. 5$     cmp.l      #$FFFF,d0                  ; Divide
  1218.      ble.b      20$                      ;
  1219.      lsr.l      #5,d0                   ;
  1220.      divu.w   d0,d1                   ;
  1221.      andi.l   #$FFFF,d1                  ;
  1222.      lsr.l      #5,d1                   ;
  1223.      bra.b      30$                      ;
  1224. 20$     divu.w   d0,d1                   ;
  1225. 30$     move.w   d1,_serper                  ; set period value
  1226. 40$
  1227.      move.l   (sp)+,d0                  ; restore D0
  1228.      ;
  1229.      ;      Determine if the buffer length is adequate for the selected CPS.
  1230.      ;      If not, use 64K for the length.
  1231.      ;
  1232.      move.l   vb_CurBaud(a5),d1              ; get current baud
  1233.      lsr.l      #2,d1                   ; divide by 4
  1234.      cmp.l      d1,d0                   ; buflen > CPS
  1235.      bhi.b      10$                      ; yep, branch
  1236.      moveq      #1,d0
  1237.      swap      d0                      ; else use 64K
  1238.      ;
  1239.      ;      Allocate new internal buffer
  1240.      ;
  1241. 10$     cmp.l      vb_CurRBufLen(a5),d0              ; len same as previous?
  1242.      beq.b      6$                      ; yep, so no need to alloc
  1243.      move.l   d0,d1                   ; save length
  1244.      move.l   a1,-(sp)                  ; save registers
  1245.      move.l   d1,-(sp)                  ; save registers
  1246.      moveq      #MEMF_PUBLIC,d1              ; public memory
  1247.      jsr      _LVOAllocMem(a6)              ; go allocate it
  1248.      move.l   (sp)+,d1                  ; restore registers
  1249.      move.l   (sp)+,a1                  ; restore registers
  1250.      tst.l      d0                      ; did we get it?
  1251.      beq.b      21$                      ; if zero, error
  1252.      move.l   d0,-(sp)                  ; save d0
  1253.      bsr.b      freeBuf                  ; go free previous buffer
  1254.      move.l   (sp)+,vb_CurRBuf(a5)              ; store new ptr
  1255.      move.l   d1,vb_CurRBufLen(a5)              ; and length
  1256.      bsr      cmd_Clear                  ; go setup buffer
  1257. 6$     moveq      #0,d0                   ; success
  1258.      bra.b      22$                      ; return
  1259. 21$     moveq      #SerErr_BufErr,d0              ; set error status
  1260. 22$
  1261.      ;
  1262.      ;      Enable and return to caller.
  1263.      ;
  1264.     IFNE DANGER
  1265.      subq.b   #1,IDNestCnt(a6)
  1266.      bge.b      enable$
  1267.      move.w   #$0C000,_intena              ; enable interrupts
  1268. enable$  rts                          ; return (D0 has status)
  1269.     ELSE
  1270.      jmp      _LVOEnable(a6)              ; enable interrupts
  1271.                               ; return (D0 has status)
  1272.     ENDC
  1273.      ;
  1274.      ;      Free internal buffer
  1275.      ;
  1276. freeBuf:
  1277.      move.l   vb_CurRBuf(a5),d0              ; is one there?
  1278.      beq.b      10$                      ; no so branch
  1279.      move.l   a1,-(sp)                  ; save registers
  1280.      move.l   d1,-(sp)                  ; save registers
  1281.      movea.l  d0,a1                   ; get ptr
  1282.      move.l   vb_CurRBufLen(a5),d0              ; get length
  1283.      clr.l      vb_CurRBuf(a5)              ; clear
  1284.      clr.l      vb_CurRBufLen(a5)              ; clear
  1285.      jsr      _LVOFreeMem(a6)              ; free it
  1286.      move.l   (sp)+,d1                  ; restore registers
  1287.      move.l   (sp)+,a1                  ; restore registers
  1288. 10$     rts                          ; return
  1289.      ;
  1290.      ;      Checks CTS status and if clear generates a TBE interrupt or
  1291.      ;      requeues the timer request.
  1292.      ;
  1293.      ;      Entered from Exec using the MsgPort callback.
  1294.      ;
  1295.      ;      Input:   a6 = ExecBase
  1296.      ;      Output:  none
  1297.      ;
  1298.      ;      No need to preserve d0/d1/a0/a1
  1299.      ;
  1300. timerRtn:
  1301.      lea.l      timerReq(pc),a1              ; get ptr to timer request
  1302.      ; This is the REMOVE macro
  1303.      move.l   (a1)+,a0
  1304.      move.l   (a1),a1                  ; LN+PRED
  1305.      move.l   a0,(a1)
  1306.      move.l   a1,LN_PRED(a0)
  1307.      ;
  1308.      ;      If we were breaking, reset adkcon.
  1309.      ;
  1310.      lea      _custom+adkconr,a0              ; get ptr to custom regs
  1311.      btst      #ADKB_UARTBRK&7,(a0)              ; were we breaking?
  1312.      beq.b      10$                      ; nope, skip reset
  1313.      ;
  1314.      move.w   #ADKF_UARTBRK,adkcon-adkconr(a0)    ; stop breaking
  1315.      ;
  1316.      ;
  1317.      ;
  1318. 10$     move.b   Handshake(pc),d0              ; are we handshaking?
  1319.      beq.b      20$                      ; nope, generate interrupt
  1320.      btst      #CIAB_COMCTS,_ciabpra           ; clear to send?
  1321.      beq.b      20$                      ; yep, go start writing
  1322.      ;
  1323.      lea.l      timerReq(pc),a1              ; get ptr to timer request
  1324.      move.l   #1000,IOTV_TIME+TV_MICRO(a1)          ; wait for .001 seconds
  1325.      jmp      _LVOSendIO(a6)              ; go queue it / return
  1326.      ;
  1327.      ;      CTS is clear so generate TBE interrupt to restart writing
  1328.      ;
  1329. 20$     move.w   #INTF_SETCLR|INTF_TBE,intreq-adkconr(a0)  ; set TBE interrupt
  1330. 30$     rts                          ; return
  1331.      ;
  1332.      ;      Non serial interrupt
  1333.      ;
  1334. level1n:
  1335.      movem.l  d1/a1/a5/a6,-(sp)              ; save registers
  1336.      move.l   d0,d1                   ; get enabled interrupts
  1337.      swap      d1
  1338.      move.l   SysBase(pc),a6              ; get ExecBase
  1339.      and.w      d0,d1                   ; and in requested interrupts
  1340.      ;
  1341.      btst      #INTB_DSKBLK,d1              ; Disk block done?
  1342.      beq.b      10$                      ; nope, branch
  1343.      ;
  1344.      movem.l  IVDSKBLK(a6),a1/a5              ; get data and code ptrs
  1345.      pea.l      20$(pc)                  ; push return address
  1346.      jmp      (a5)                      ; jump to routine
  1347.      ;
  1348. 10$
  1349.      ;
  1350.      movem.l  IVSOFTINT(a6),a1/a5              ; get data and code ptrs
  1351.      jsr      (a5)                      ; jump to routine
  1352.      ;
  1353. 20$     movem.l  (sp)+,d1/a1/a5-a6              ; restore registers
  1354.      move.l   (sp)+,d0                  ; restore registers
  1355.      move.l   (sp)+,a0                  ; restore registers
  1356.      rte                          ; return
  1357.      ;
  1358.      ;      Level 1 interrupt handler
  1359.      ;
  1360. level1:
  1361.      move.l   a0,-(sp)                  ; save A0 (faster than MOVEM)
  1362.      move.l   d0,-(sp)                  ; save D0 (faster than MOVEM)
  1363.      lea      _custom,a0                  ; get ptr to custom regs
  1364.      move.l   intenar(a0),d0              ; get intenar & intreqr
  1365.      btst      #INTB_INTEN+16,d0              ; interrupts enabled?
  1366.      beq.b      30$                      ; nope, ignore
  1367.      btst      #INTB_TBE,d0                  ; xmit buffer empty?
  1368.      beq.b      level1n                  ; nope, invoke old handler
  1369.      ;
  1370.      ;      Handle "Transmit Buffer Empty" interrupt (write)
  1371.      ;
  1372.      move.w   #INTF_TBE,intreq(a0)              ; clear interrupt
  1373.      ;
  1374.      ;      If we're not handshaking, bypass it.
  1375.      ;
  1376. 10$     move.b   Handshake(pc),d0              ; are we handshaking?
  1377.      beq.b      20$                      ; nope, skip CTS test
  1378.      btst      #CIAB_COMCTS,_ciabpra           ; clear to send?
  1379.      bne.b      40$                      ; nope, branch
  1380.      ;
  1381.      ;      If cw_Length goes negative here, we are either done with a
  1382.      ;      request or we were called as a result of a fake interrupt
  1383.      ;      to force us to get the next request going.
  1384.      ;
  1385. 20$     subq.l   #1,cw_Length                  ; decr write length
  1386.      blt.b      60$                      ; < zero, done, branch
  1387.      ;
  1388.      ;      Currently processing a request.
  1389.      ;
  1390.      movea.l  cw_Buffer(pc),a0              ; get buffer ptr
  1391.      move.w   #256,d0                  ; set stop bit
  1392.      move.b   (a0)+,d0                  ; get next byte
  1393.      move.l   a0,cw_Buffer                  ; store buffer ptr
  1394.      move.w   d0,_serdat                  ; store in serdat reg
  1395.      ;
  1396. 30$     move.l   (sp)+,d0                  ; restore registers
  1397.      move.l   (sp)+,a0                  ; restore registers
  1398.      rte                          ; return
  1399.      ;
  1400.      ;      Queue a timer request to recheck CTS status
  1401.      ;
  1402. 40$     movem.l  d1/a1/a6,-(sp)              ; save registers
  1403.      lea.l      timerReq(pc),a1              ; get ptr to timer request
  1404.      move.l   #1000,IOTV_TIME+TV_MICRO(a1)          ; wait for .001 seconds
  1405.      movea.l  SysBase(pc),a6              ; get ExecBase
  1406.      jsr      _LVOSendIO(a6)              ; queue the request
  1407.      move.l   (sp)+,d1                  ; restore registers
  1408.      move.l   (sp)+,a1                  ; restore registers
  1409.      move.l   (sp)+,a6                  ; restore registers
  1410.      bra.b      30$                      ; go return
  1411.      ;
  1412.      ;      There aren't anymore requests, so clear and exit
  1413.      ;
  1414. 50$     clr.l      cw_Length-Start(a6)              ; clear length
  1415.      clr.l      cw_IOReq-Start(a6)              ; clear
  1416.      move.l   (sp)+,d1                  ; restore registers
  1417.      move.l   (sp)+,a1                  ; restore registers
  1418.      move.l   (sp)+,a6                  ; restore registers
  1419.      bra.b      30$                      ; go return
  1420.      ;
  1421.      ;      Write request completed
  1422.      ;
  1423. 60$     movem.l  d1/a1/a6,-(sp)              ; save registers
  1424.      ;
  1425.      move.l   cw_IOReq(pc),d0              ; active I/O request?
  1426.      beq.b      70$                      ; nope, branch
  1427.      ;
  1428.      ;      Reply it and setup for next
  1429.      ;
  1430.      movea.l  d0,a1                   ; get I/O request
  1431.      andi.b   #~(1<<IOSERB_ACTIVE),IO_FLAGS(a1)   ; no longer active
  1432.      clr.b      IO_ERROR(a1)                  ; no error
  1433.      movea.l  SysBase(pc),a6              ; get ExecBase
  1434.      jsr      _LVOReplyMsg(a6)              ; return I/O request
  1435.      ;
  1436. 70$     lea.l      Start(pc),a6                  ; get section base
  1437.      ;
  1438.      lea.l      writeQ(pc),a1               ; get ptr to write queue
  1439.      move.l   (a1),a0                  ; get head of list
  1440.      move.l   (a0),d0                  ; get successor
  1441.      beq.b      50$                      ; end of list? yep, branch
  1442.      ;
  1443.      ;      Remove the node from the list
  1444.      ;
  1445.      move.l   d0,(a1)                  ; make new head
  1446.      exg.l      d0,a0                   ; swap nodes
  1447.      move.l   a1,LN_PRED(a0)              ; store predecessor
  1448.      ;
  1449.      move.l   d0,a1                   ; get I/O request
  1450.      ;
  1451.      ;      If it's a BREAK, then branch to process as such.
  1452.      ;
  1453.      cmpi.w   #SDCMD_BREAK,IO_COMMAND(a1)          ; BREAK command?
  1454.      beq.b      100$                      ; yep, branch
  1455.      ;
  1456.      ;      Check for absolute length
  1457.      ;
  1458.      move.l   IO_DATA(a1),a0              ; get data ptr
  1459.      move.l   IO_LENGTH(a1),d0              ; get length
  1460.      ;
  1461. 90$     move.l   d0,IO_ACTUAL(a1)              ; go ahead and set it
  1462.      move.b   IO_FLAGS(a1),d1
  1463.      move.l   d0,cw_Length-Start(a6)          ; store length
  1464.      andi.b   #~(1<<IOSERB_QUEUED),d1          ; no longer queued
  1465.      move.l   a0,cw_Buffer-Start(a6)          ; store buffer ptr
  1466.      ori.b      #1<<IOSERB_ACTIVE,d1              ; make it active
  1467.      move.l   a1,cw_IOReq-Start(a6)           ; store I/O request ptr
  1468.      move.b   d1,IO_FLAGS(a1)
  1469.      ;
  1470.      move.l   (sp)+,d1                  ; restore registers
  1471.      move.l   (sp)+,a1                  ; restore registers
  1472.      move.l   (sp)+,a6                  ; restore registers
  1473.      bra      10$                      ; go start request
  1474.      ;
  1475.      ;      Start the BREAK.
  1476.      ;
  1477. 100$     move.b   IO_FLAGS(a1),d1
  1478.      clr.l      cw_Length-Start(a6)              ; clear length
  1479.      andi.b   #~(1<<IOSERB_QUEUED),d1          ; no longer queued
  1480.      move.l   a1,cw_IOReq-Start(a6)           ; store I/O request ptr
  1481.      ori.b      #1<<IOSERB_ACTIVE,d1              ; make it active
  1482.      move.w   #ADKF_SETCLR|ADKF_UARTBRK,_adkcon   ; start break
  1483.      move.b   d1,IO_FLAGS(a1)
  1484.      move.l   IO_BRKTIME(a1),d0              ; get break time
  1485.      lea.l      timerReq(pc),a1              ; get ptr to timer request
  1486.      move.l   d0,IOTV_TIME+TV_MICRO(a1)          ; set the timeout
  1487.      movea.l  SysBase(pc),a6              ; get ExecBase
  1488.      jsr      _LVOSendIO(a6)              ; queue the request
  1489.      move.l   (sp)+,d1                  ; restore registers
  1490.      move.l   (sp)+,a1                  ; restore registers
  1491.      move.l   (sp)+,a6                  ; restore registers
  1492.      bra      30$                      ; go exit
  1493.      ;
  1494.      ;    Non serial interrupt
  1495.      ;    Handles:
  1496.      ;        INTB_DSKSYNC
  1497.      ;
  1498.      ;  a0 - custom chips base
  1499.      ;
  1500. level5n:
  1501.      movem.l  d1/a1/a5/a6,-(sp)              ; save registers
  1502.      move.l   d0,d1
  1503.      move.l   SysBase(pc),a6              ; get ExecBase
  1504.      swap      d1
  1505.      movem.l  IVDSKSYNC(A6),a1/a5              ; get data and code ptrs
  1506.      and.w      d0,d1
  1507.      lea      _custom,a0                  ; get ptr to custom regs
  1508.      jsr      (a5)                      ; branch to routine
  1509.      movem.l  (sp)+,d1/a1/a5/a6              ; restore registers
  1510.      move.l   (sp)+,a0                  ; restore registers
  1511.      move.l   (sp)+,d0                  ; restore registers
  1512.      rte                          ; return
  1513.      ;
  1514.      ;      Default Level 5 handler
  1515.      ;
  1516. level5:
  1517.      move.l   d0,-(sp)                  ; save registers
  1518.      move.l   a0,-(sp)                  ; save registers
  1519.      lea      _custom+serdatr,a0              ; get ptr to custom regs
  1520.      ;
  1521.      move.l   intenar-serdatr(a0),d0          ; get intenar & intreqr
  1522.      btst      #INTB_INTEN+16,d0              ; interrupts enabled?
  1523.      beq.b      41$                      ; nope, ignore
  1524.      btst      #INTB_RBF,d0                  ; receive buffer full?
  1525.      beq.b      level5n                  ; nope, invoke old handler
  1526.      ;
  1527. 10$     move.w   (a0),d0                  ; Overrun?
  1528.      bmi.b      90$                      ; nope, branch
  1529. 20$     move.w   #INTF_RBF,intreq-serdatr(a0)          ; clear RBF interrupt
  1530.      move.l   i_BufIn(pc),a0              ; get current ptr
  1531.      move.b   d0,(a0)+                  ; store received byte
  1532.      addq.l   #1,i_InCnt                  ; incr bytes in buffer
  1533.      ;
  1534.      cmpa.l   i_BufEnd(pc),a0              ; hit end of buffer?
  1535.      beq.b      91$                      ; yep, branch
  1536. 30$     move.l   a0,i_BufIn                  ; store input ptr
  1537.      ;
  1538.      subq.l   #1,i_Thresh                  ; close to full buffer?
  1539.      beq.b      92$                      ; yep, branch
  1540.      ;
  1541. 40$
  1542.     IFEQ NEWCODE
  1543.      lea      _custom+serdatr,a0              ; get ptr to custom regs
  1544.      btst      #INTB_RBF&7,intreqr-serdatr(a0)     ; receive buffer full?
  1545.      bne.b      10$                      ; yep, go get another byte
  1546.     ENDC
  1547.      ;
  1548. 41$     movea.l  (sp)+,a0                  ; restore registers
  1549.      movea.l  (sp)+,d0                  ; restore registers
  1550.      rte                          ; return
  1551.      ;
  1552.      ;      We've missed some data, so set overrun flag.
  1553.      ;
  1554. 90$     addq.b   #1,Overrun                  ; set overrun flag
  1555.      bra.b      20$
  1556.      ;
  1557.      ;      Hit physical end of buffer, so wrap to the start of the buffer.
  1558.      ;
  1559. 91$     movea.l  i_BufPtr(pc),a0              ; get buffer ptr
  1560.      bra.b      30$
  1561.      ;
  1562.      ;      Hit buffer threshold, so tell other end not to send any more
  1563.      ;      data.
  1564.      ;
  1565. 92$     tst.b      Handshake                  ; are we handshaking?
  1566.      beq.b      40$                      ; nope, skip RTS
  1567.      ori.b      #1<<CIAB_COMRTS,_ciabpra          ; block further input
  1568.      bra.b      40$
  1569.      ;
  1570.      ;
  1571.      ;
  1572. level2:
  1573.      ;
  1574.      ;      If there's nothing in the buffer, there's no point in going
  1575.      ;      any further.
  1576.      ;
  1577.      move.l   i_InCnt(pc),d0              ; anything in the buffer?
  1578.      beq.b      9$                      ; nope, branch
  1579.      ;
  1580.      ;      If we've been "disabled" then get out.
  1581.      ;
  1582. 20$     move.b   disableRead(pc),d0              ; internally disabled?
  1583.      bge.b      10$                      ; yep, get out of here
  1584.      lea.l      Start(pc),a5                  ; get base
  1585.      ;
  1586.      ;      If we have an active request, branch down and try to fulfill it.
  1587.      ;
  1588.      move.l   cr_IOReq(pc),d0              ; get and test active I/O
  1589.      bne.b      30$                      ; nzero, active, branch
  1590.      ;
  1591.      ;      Get first node in list and test if empty.
  1592.      ;
  1593.      move.l   (a1),a0                  ; get head of list
  1594.      move.l   (a0),d0                  ; get successor
  1595.      beq.b      9$                      ; end of list? yep, branch
  1596.      ;
  1597.      ;      Remove the node from the list
  1598.      ;
  1599.      move.l   d0,(a1)                  ; make new head
  1600.      exg.l      d0,a0                   ; swap nodes
  1601.      move.l   a1,LN_PRED(a0)              ; store predecessor
  1602.      ;
  1603.      ;      Setup fields for processing a read request
  1604.      ;
  1605.      movea.l  d0,a1                   ; get I/O request
  1606.      move.b   IO_FLAGS(a1),d1
  1607.      andi.b   #~(1<<IOSERB_QUEUED),d1          ; no longer queued
  1608.      move.l   a1,cr_IOReq-Start(a5)           ; store I/O request
  1609.      ori.b      #1<<IOSERB_ACTIVE,d1              ; make it active
  1610.      move.l   IO_DATA(a1),cr_OutPtr-Start(a5)     ; get/set output ptr
  1611.      move.b   d1,IO_FLAGS(a1)
  1612.      move.l   IO_LENGTH(a1),cr_Length-Start(a5)   ; get/set output count
  1613.      ;
  1614.      ;      Process an active I/O request
  1615.      ;
  1616. 30$     movea.l  SysBase(pc),a6              ; get ExecBase
  1617.      move.l   d0,a1                   ; get I/O request
  1618.      bsr.b      copyData                  ; go copy 'em
  1619.      tst.l      d0                      ; done with request?
  1620.      beq.b      9$                      ; nope, branch
  1621.      ;
  1622.      ;      the request has been satisfied, so return it.
  1623.      ;
  1624.      clr.l      cr_IOReq-Start(a5)              ; clear request
  1625.      andi.b   #~(1<<IOSERB_ACTIVE),IO_FLAGS(a1)   ; no longer active
  1626.      jsr      _LVOReplyMsg(a6)              ; return I/O
  1627. 10$     moveq      #0,d0                   ; set Z flag
  1628. 9$     rts                          ; return
  1629.      ;
  1630.      ;      Registers:
  1631.      ;      Entry:   A1        Ptr to I/O Request
  1632.      ;           A6        ExecBase
  1633.      ;      Exit:    D0        ZERO - request not done, do not reply it
  1634.      ;                NZERO - request done, reply it
  1635.      ;
  1636. copyData:
  1637.      ;
  1638.      ;      Process an active I/O request (or fall through from above)
  1639.      ;
  1640.      movem.l  d2-d5/a1-a5,-(sp)              ; save registers
  1641.      lea.l      Start(pc),a5                  ; get base
  1642.      movea.l  a1,a4                   ; get I/O request
  1643.      ;
  1644.      movea.l  i_BufOut(pc),a2              ; get current bufout ptr
  1645.      movea.l  cr_OutPtr(pc),a3              ; get current output ptr
  1646.      move.l   cr_Length(pc),d2              ; get current bytes needed
  1647.      move.l   i_InCnt(pc),d3              ; get current bytes in buffer
  1648.      ;
  1649.      ;      If we don't have enough bytes to satisfy the request,
  1650.      ;      set the length to the number of bytes we do have.
  1651.      ;
  1652.      cmp.l      d2,d3                   ; enuf to satisfy request?
  1653.      bge.b      15$                      ; yep, so branch
  1654.      move.l   d3,d2                   ; # to copy = # in buffer
  1655.      bra.b      16$                      ; branch to loop entry
  1656.      ;
  1657.      ;      Start of copy loop.
  1658.      ;
  1659. 10$     movea.l  i_BufPtr(pc),a2              ; reset bufout to start
  1660.      ;
  1661.      ;      Entry point of copy loop.
  1662.      ;
  1663. 15$     move.l   d2,d3                   ; xfer # of bytes to copy
  1664. 16$
  1665.      ;
  1666.      ;      If the copy will extend past the end of the buffer, we can
  1667.      ;      only copy the number of bytes to the end this go around.
  1668.      ;
  1669.      move.l   i_BufEnd(pc),d0              ; get ptr to end of buf
  1670.      sub.l      a2,d0                   ; calc # of bytes to end
  1671.      cmp.l      d0,d3                   ; # to copy < # to end?
  1672.      blt.b      20$                      ; yep, branch
  1673.      move.l   d0,d3                   ; get bytes to end
  1674.      ;
  1675.      ;      Registers:
  1676.      ;
  1677.      ;      A2 = pointer from which data will be copied
  1678.      ;      A3 = pointer to which data will be copied
  1679.      ;      D2 = number of bytes that need to be copied
  1680.      ;      D3 = number of bytes to copy this iteration
  1681.      ;
  1682. 20$     move.l   d3,d0                   ; get length
  1683.     IFNE EOFCODE
  1684.      btst      #SERB_EOFMODE,IO_SERFLAGS(a4)       ; EOFMODE requested?
  1685.      beq.b      30$                      ; nope, just go copy
  1686.      ;
  1687.      ;      EOFMODE was specified so copy characters 1 at a time until
  1688.      ;      we hit an EOF character, the output butter has filled, or
  1689.      ;      the input buffer has drained.
  1690.      ;
  1691.      move.l   IO_TERMARRAY(a4),d4              ; get termarry (part 1)
  1692.      rol.l      #8,d4
  1693.      move.l   IO_TERMARRAY(a4),d5              ; get termarry (part 2)
  1694.      move.l   d4,a1                   ; save d4
  1695.      rol.l      #8,d5
  1696.      ;
  1697. 21$     move.b   (a2)+,d1                  ; get byte
  1698.      ; This way the term char IS copied (if the term char is not copied eofmode will fail)
  1699.      move.b   d1,(a3)+                  ; put in output buffer
  1700.      ;
  1701.      cmp.b      d4,d1                   ; found term char?
  1702.      bge.b      22$                      ; possibly, branch
  1703.      rol.l      #8,d4
  1704.      cmp.b      d4,d1                   ; found term char?
  1705.      bge.b      22$                      ; possibly, branch
  1706.      rol.l      #8,d4
  1707.      cmp.b      d4,d1                   ; found term char?
  1708.      bge.b      22$                      ; possibly, branch
  1709.      rol.l      #8,d4
  1710.      cmp.b      d4,d1                   ; found term char?
  1711.      bge.b      22$                      ; possibly, branch
  1712.      move.l   d5,d4                   ; get termarry (part 2)
  1713.      cmp.b      d4,d1                   ; found term char?
  1714.      bge.b      22$                      ; possibly, branch
  1715.      rol.l      #8,d4
  1716.      cmp.b      d4,d1                   ; found term char?
  1717.      bge.b      22$                      ; possibly, branch
  1718.      rol.l      #8,d4
  1719.      cmp.b      d4,d1                   ; found term char?
  1720.      bge.b      22$                      ; possibly, branch
  1721.      rol.l      #8,d4
  1722.      cmp.b      d4,d1                   ; found term char?
  1723. 22$     beq.b      24$                      ; term char found?
  1724.      ;
  1725.      ;      Didn't find a term character, so continue with the copy loop
  1726.      ;
  1727. 23$     move.l   a1,d4                   ; get termarry (part 1)
  1728.      subq.l   #1,d0                   ; decr length counter
  1729.      bne.b      21$                      ; continue if more
  1730.      bra.b      40$                      ; done with copy, branch
  1731.      ;
  1732.      ;      We've found a termination character.
  1733.      ;
  1734. 24$     clr.l      cr_Length-Start(a5)              ; done with request
  1735.      bra.b      50$                      ; branch
  1736.     ENDC
  1737.      ;
  1738.      ;      EOFMODE not specified, so just do a bulk copy.
  1739.      ;
  1740.      ;      XXX POSSIBLE SPEEDUP XXX
  1741.      ;
  1742.      ;      For short copies it would be quicker to have a simple inline
  1743.      ;      loop, but what's short???  It would be different by CPU.
  1744.      ;
  1745. 30$     movea.l  a3,a1                   ; where to put it
  1746.      movea.l  a2,a0                   ; where to get it
  1747. ; NB: buffer size is multiple of 64bytes (not contents size)
  1748.      jsr      _LVOCopyMem(a6)              ; copy it
  1749.      adda.l   d3,a2                   ; update bufout
  1750.      adda.l   d3,a3                   ; update outptr
  1751.      ;
  1752.      ;      Fall through and entered from EOFMODE loop
  1753.      ;
  1754.      ;      If the following calculation results in a value greater than
  1755.      ;      zero, then we have a buffer wrap and need to process the
  1756.      ;      remaining bytes at the beginning of the buffer.
  1757.      ;
  1758. 40$     sub.l      d3,d2                   ; calc bytes left to copy
  1759.      bgt.b      10$                      ; >0, more to copy, branch
  1760.      ;
  1761.      ;
  1762.      ;
  1763. 50$     move.l   a3,d1                   ; get outptr
  1764.      sub.l      cr_OutPtr(pc),d1              ; calc length
  1765.      move.l   a3,cr_OutPtr-Start(a5)          ; update outptr
  1766.      ;
  1767.      ;      Update output ptr and I/O request
  1768.      ;
  1769.      move.l   a2,i_BufOut-Start(a5)           ; store bufout ptr
  1770.      add.l      d1,IO_ACTUAL(a4)              ; update I/O request
  1771.      ;
  1772.      ;      Update number of bytes left in the buffer.
  1773.      ;
  1774.      sub.l      d1,i_InCnt-Start(a5)              ; calc bytes left in buffer
  1775.      ;
  1776.      ;      If the threshold becomes positive here, then, if requested,
  1777.      ;      tell the other end that it's okay to start sending more data.
  1778.      ;
  1779.      add.l      d1,i_Thresh-Start(a5)           ; calc thresh and test
  1780.      ble.b      60$                      ; <= 0, need more data, skip
  1781.      tst.b      Handshake-Start(a5)              ; are we handshaking?
  1782.      beq.b      60$                      ; nope, skip RTS
  1783.      andi.b   #~(1<<CIAB_COMRTS),_ciabpra          ; ready to receive more data
  1784.      ;
  1785.      ;      Set error if we've had any overruns.
  1786.      ;
  1787. 60$     tst.b      Overrun-Start(a5)              ; did overrun occur?
  1788.      bne.b      100$                      ; yep, branch
  1789.      ;
  1790.      ;      Calc number of bytes left to copy.  If the result is greater
  1791.      ;      than zero, then we have more to copy so return an incomplete
  1792.      ;      status.
  1793.      ;
  1794. 70$     moveq      #0,d0                   ; assume I/O incomplete
  1795.      sub.l      d1,cr_Length-Start(a5)          ; update length and test
  1796.      bgt.b      90$                      ; >0, more to do, branch
  1797.      ;
  1798.      ;      We've completed the I/O request either by copying the requested
  1799.      ;      of bytes or by finding an EOFMODE character, so return a "reply"
  1800.      ;      status.
  1801.      ;
  1802.      moveq      #1,d0                   ; indicate reply
  1803.      ;
  1804.      ;      Restore and exit
  1805.      ;
  1806. 90$     movem.l  (sp)+,d2-d5/a1-a5              ; restore registers
  1807.      rts                          ; return (status in D0)
  1808. 100$     clr.b      Overrun-Start(a5)              ; reset overrun flag
  1809.      move.b   #SerErr_LineErr,IO_ERROR(a4)          ; set error code
  1810.      bra.b      70$
  1811.      ;
  1812.      ;      Align data
  1813.      ;
  1814.      CNOP      0,4
  1815.      ;
  1816.      ;
  1817.      ;
  1818. Init:
  1819.      DC.L      sizeof_Base8n1
  1820.      DC.L      funcTab
  1821.      DC.L      dataTab
  1822.      DC.L      InitRoutine
  1823.      ;
  1824.      ;
  1825.      ;
  1826. funcTab:
  1827.      DC.W      -1
  1828.      DC.W      dev_Open-funcTab
  1829.      DC.W      dev_Close-funcTab
  1830.      DC.W      dev_Expunge-funcTab
  1831.      DC.W      dev_Null-funcTab
  1832.      DC.W      dev_BeginIO-funcTab
  1833.      DC.W      dev_AbortIO-funcTab
  1834.      DC.W      -1
  1835.      ;
  1836.      ;
  1837.      ;
  1838. dataTab:
  1839.      INITBYTE LN_TYPE,NT_DEVICE
  1840.      INITLONG LN_NAME,Name
  1841.      INITBYTE LIB_FLAGS,LIBF_SUMUSED|LIBF_CHANGED
  1842.      INITWORD LIB_VERSION,VERSION
  1843.      INITWORD LIB_REVISION,REVISION
  1844.      INITLONG LIB_IDSTRING,IdString
  1845.      DC.W      0
  1846.      ;
  1847.      ;      String Constants
  1848.      ;
  1849. miscresource:
  1850.      DC.B      "misc.resource",0
  1851. timerdevice:
  1852.      DC.B      "timer.device",0
  1853. intuitlib:
  1854.      DC.B      "intuition.library",0
  1855. Name:
  1856.      DC.B      "8n1.device",0
  1857. IdString:
  1858.      VSTRING
  1859.      ;
  1860.      ;      End of checksummed area.  (Realigns data too!)
  1861.      ;
  1862. ENDTag:
  1863.      CNOP      0,4
  1864.      ;
  1865.      ;      Global SysBase (Use instead of AbsExecBase for speed)
  1866.      ;
  1867. SysBase:
  1868.      DC.L      0
  1869.      ;
  1870.      ;      Internal buffer tracking (DO NOT CHANGE THE ORDER!!!!)
  1871.      ;
  1872. i_BufPtr:
  1873.      DC.L      0
  1874. i_BufIn:
  1875.      DC.L      0
  1876. i_BufOut:
  1877.      DC.L      0
  1878. i_BufEnd:
  1879.      DC.L      0
  1880. i_InCnt:
  1881.      DC.L      0
  1882. i_Thresh:
  1883.      DC.L      0
  1884.      ;
  1885.      ;      Used while processing a read request.
  1886.      ;
  1887. cr_IOReq:
  1888.      DC.L      0
  1889. cr_OutPtr:
  1890.      DC.L      0
  1891. cr_Length:
  1892.      DC.L      0
  1893.      ;
  1894.      ;      List head for read requests
  1895.      ;
  1896. readQ:
  1897.      DC.L      readQ+MLH_TAIL
  1898.      DC.L      0
  1899.      DC.L      readQ
  1900.      ;
  1901.      ;      Write control.
  1902.      ;
  1903. cw_Length:
  1904.      DC.L      0
  1905. cw_Buffer:
  1906.      DC.L      0
  1907. cw_IOReq:
  1908.      DC.L      0
  1909.      ;
  1910.      ;      List head for write requests
  1911.      ;
  1912. writeQ:
  1913.      DC.L      writeQ+MLH_TAIL
  1914.      DC.L      0
  1915.      DC.L      writeQ
  1916.      ;
  1917.      ;
  1918.      ;
  1919. timerPort:
  1920.      DC.L      0                      ; LN_SUCC
  1921.      DC.L      0                      ; LN_PRED
  1922.      DC.B      NT_MSGPORT                  ; LN_TYPE
  1923.      DC.B      0                      ; LN_PRI
  1924.      DC.L      0                      ; LN_NAME
  1925.      DC.B      3                      ; MP_FLAGS (undoc'ed)
  1926.      DC.B      0                      ; MP_SIGBIT
  1927.      DC.L      timerRtn                  ; MP_SIGTASK
  1928.      DC.L      timerPort+MP_MSGLIST+LH_TAIL          ; LH_HEAD
  1929.      DC.L      0                      ; LH_TAIL
  1930.      DC.L      timerPort+MP_MSGLIST              ; LH_TAILPRED
  1931.      DC.B      0                      ; LH_TYPE
  1932.      DC.B      0                      ; LH_pad
  1933.      DC.W      0                      ; long align
  1934.      ;
  1935.      ;
  1936.      ;
  1937. timerReq:
  1938.      DC.L      0                      ; LN_SUCC
  1939.      DC.L      0                      ; LN_PRED
  1940.      DC.B      NT_MESSAGE                  ; LN_TYPE
  1941.      DC.B      0                      ; LN_PRI
  1942.      DC.L      0                      ; LN_NAME
  1943.      DC.L      timerPort                  ; MN_REPLYPORT
  1944.      DC.W      IOTV_SIZE                  ; MN_LENGTH
  1945.      DC.L      0                      ; IO_DEVICE
  1946.      DC.L      0                      ; IO_UNIT
  1947.      DC.W      TR_ADDREQUEST               ; IO_COMMAND
  1948.      DC.B      0                      ; IO_FLAGS
  1949.      DC.B      0                      ; IO_ERROR
  1950.      DC.L      0                      ; TV_SECS
  1951.      DC.L      0                      ; TV_MICROS
  1952.      ;
  1953.      ;
  1954.      ;
  1955. VBInterrupt:
  1956.      DC.L      0                      ; LN_SUCC
  1957.      DC.L      0                      ; LN_PRED
  1958.      DC.B      NT_INTERRUPT                  ; LN_TYPE
  1959.      DC.B      0                      ; LN_PRI
  1960.      DC.L      Name                      ; LN_NAME
  1961.      DC.L      readQ                   ; IS_DATA
  1962.      DC.L      level2                  ; IS_CODE
  1963.      ;
  1964.      ;      Global flags
  1965.      ;
  1966. Overrun:
  1967.      DC.B      0
  1968. Handshake:
  1969.      DC.B      1
  1970. disableRead:
  1971.      DC.B      -1
  1972.      CNOP      0,4
  1973. cmdlist:
  1974.      dc.w      CMD_RESET
  1975.      dc.w      CMD_READ
  1976.      dc.w      CMD_WRITE
  1977.      dc.w      CMD_CLEAR
  1978.      dc.w      CMD_FLUSH
  1979.      dc.w      SDCMD_QUERY
  1980.      dc.w      SDCMD_BREAK
  1981.      dc.w      SDCMD_SETPARAMS
  1982.      dc.w      NSCMD_DEVICEQUERY
  1983.      dc.w      0
  1984.      ;
  1985.      ;
  1986.      ;
  1987.      END
  1988.  
  1989.